package com.example.springdata_repositories.service.impl;

import com.example.springdata_repositories.domain.Students;
import com.example.springdata_repositories.repository.StudentsRepository;
import com.example.springdata_repositories.service.StudentsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

@Service
public class StudentsServiceImpl implements StudentsService {


    @Resource
    private StudentsRepository studentsRepository;

    @Override
    public Students saveStudents(Students s) throws Exception {
        Students stu = studentsRepository.save(s);
        return stu;
    }

    @Override
    public Students queryStudentsBySid(String sid) {
        Optional<Students> studentsOptional = studentsRepository.findById(sid);
        return studentsOptional.orElse(null);
    }

    @Override
    public Students updateStudents(Students s) throws Exception {
        Students stu = studentsRepository.save(s);
        return stu;
    }

    @Override
    public boolean removeStudents(String sid) throws Exception {
        try {
            studentsRepository.deleteById(sid);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }
}
